package com.lzq.leetcode.bat.juc;

import java.util.concurrent.Semaphore;

public class PrintAB {

    private static final Semaphore sA = new Semaphore(1);
    private static final Semaphore sB = new Semaphore(0);

    public static void main(String[] args) {

        new Thread(() -> {
            try {
                while (true) {
                    sA.acquire();
                    System.out.println("A");
                    sB.release();
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }).start();

        new Thread(() -> {
            try {
                while (true) {
                    sB.acquire();
                    System.out.println("B");
                    sA.release();
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }).start();
    }
}
